﻿
//通用的对菜单项进行处理的方法。
//需要参数: 包含菜单信息的 对象数组(可以从JSON 得到), 用于进行建立菜单项的方法(该方法接收一个包含MenuItem 信息的 对象作为参数）
function buildMenuItemContent(menuItemSource, contentTempleteBuilder) {
 
     if (menuItemSource == null || menuItemSource == undefined || menuItemSource.length == 0) { 
 
    	return false;
    }
 
    var result = "";
    var menuItem = { Id: 0, ParentId: 0, Name: "test", PageUrl: "javascript:void(0);", Index: 0 };
    for (var index in menuItemSource) {
        menuItem.Name = menuItemSource[index].limitName;
        menuItem.PageUrl = menuItemSource[index].limitUrl;
        menuItem.Id = menuItemSource[index].limitId;
        menuItem.ParentId = menuItemSource[index].parentId;
        menuItem.Index = index;
     
        result += contentTempleteBuilder(menuItem);
    }
    menuItem = null; 
   
    return result;
}


//Post 加载菜单..并返回菜单对象数组..
//参数:
//        0. markObjectId ; 进行提示"菜单正在加载的 html 对象(jQuery 对象);
//        1. partentId ; ParentId;
//        2. afterLoadedMethod ; 在成功获得菜单对象数组之后对其进行处理的方法, 接收一个 参数, 包含Parent 对象和 菜单对象数组.
function ajaxLoadMenus(markObject, parentId, afterLoadedMethod) {
  var markuper = $(markObject);

  $(markObject).html('<span class="red-tips"><img src="themes/icons/loadingObject.gif" alt="加载中..."></img></span>');

     $.post("limit.action",
       		{ "parentId": parentId },
     		 function (data) {
     		     data = $.toJSON(data);
     		     var menus = eval(data);
    		     afterLoadedMethod(menus);
    		      markuper.html("").remove();
      		 }, "json"); 
}


//------------------以上两个为公共方法..

//.....................................


//根菜单
function buildRootMenuItems(menus) {
    var mainMenuPlaceHolder = $("#MainMenu"); 

    if (menus == null || menus == undefined || menus.length == 0) {
        mainMenuPlaceHolder.html('<div class="red-tips">加载菜单失败.请重新<a class="originalTipsLink" href="login!login.action">登录</a>.</div>');
        return;
    }
      var result = buildMenuItemContent(menus, function (menuItem) {  
        return '<div class="Pnav_title"><span hasLoad="false" menuId="' + menuItem.Id + '" Parent="0">' + menuItem.Name + '</span></div><div class="Pnav_item"></div><div class="Pnav_foot"></div>';
    }); 
    
    mainMenuPlaceHolder.html(result);
}

//创建二级菜单
function BuildChildMenus(menus, parent) { 
    var mainMenuPlaceHolder = parent; 

    if (menus == null || menus == undefined || menus.length == 0) {
        return;
    }
    var result = buildMenuItemContent(menus, function (menuItem) {
        return '<div class="Node" hasnode="false" hasLoad="false" menuId="' + menuItem.Id + '" ParentId="' + menuItem.ParentId + '"><a href="' + menuItem.PageUrl + '" target="main">' + menuItem.Name + '</a></div><div class="childNodes" style=""></div>';
    });
    mainMenuPlaceHolder.html(result);

}


//创建孙子菜单..
function BuildGrandSon(menus, parent) {

  if (menus == null || menus == undefined || menus.length == 0) {
      return false;
  }
  var result = buildMenuItemContent(menus, function (menuItem) {
      return '<div class="Node" hasLoad="false" menuId="' + menuItem.Id + '" ParentId="' + menuItem.ParentId + '"><a href="' + menuItem.PageUrl + '" target="main">' + menuItem.Name + '</a></div>';
  });

  parent.html(result);
  parent.show();
  return true;
}
